<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #tips {
            height: 40px;
            line-height: 40px;
            text-indent: 10px;
            font-size: 24px;
        }
    </style>
</head>

<body>
    <div id="tips"></div>
</body>
<script>
    // 倒计时
    // 当前时间(now)        起始时间(start) <  结束时间(end)

    var tips = document.getElementById("tips");

    // 页面加载时  获取一次
    timeCount();
    setInterval(timeCount, 1000);

    function timeCount() {
        var start = new Date("2022-6-18");
        var end = new Date("2022-6-19");

        var start = new Date("2022-6-13 10:35:00");
        var end = new Date("2022-6-13 10:36:00");

        var now = new Date();   // 当前时间

        var beforeStart = start - now;   // 毫秒差
        var beforeStart = parseInt(beforeStart / 1000); //秒差

        var beforeEnd = end - now;
        var beforeEnd = parseInt(beforeEnd / 1000); //秒差

        console.log(beforeStart);  // 395792

        if (beforeStart > 0) { // now < start  活动还未开始
            var day = parseInt(beforeStart / (60 * 60 * 24)); // 4 
            console.log(day);

            // var hour = parseInt((beforeStart - day * (60 * 60 * 24)) / (60 * 60));
            // 总的时间 % 一天的时间  => 不满一天(秒)
            // 不满一天 / (一小时)   => 小时的倍数

            var hour = parseInt((beforeStart % (60 * 60 * 24)) / (60 * 60));
            console.log(hour);

            // 总的时间 / 一小时的时间  => 小数的总倍数
            // 小数的总倍数 % 24 => 不满一天的小时数
            // var hour = beforeStart / (60 * 60)

            var minute = parseInt(beforeStart % (60 * 60) / 60);
            console.log(minute);

            var second = beforeStart % 60;

            // 距离618还有xx天xx时xx分xx秒
            tips.innerText = "距离618开始,还有" + day + "天" + hour + "时" + minute + "分" + second + "秒";
        } else if (beforeStart <= 0 && beforeEnd > 0) { //  now > start & now < end 活动正在进行中
            var day = parseInt(beforeEnd / (60 * 60 * 24)); // 4 
            console.log(day);

            var hour = parseInt((beforeEnd % (60 * 60 * 24)) / (60 * 60));
            console.log(hour);

            var minute = parseInt(beforeEnd % (60 * 60) / 60);
            console.log(minute);

            var second = beforeEnd % 60;

            // 距离618还有xx天xx时xx分xx秒
            tips.innerText = "618已开始,距离活动结束还有" + day + "天" + hour + "时" + minute + "分" + second + "秒";
        } else {  // 活动已经结束
            tips.innerText = "活动已经结束";
        }
    }






</script>

</html>